package com.yidu.controller;

import com.yidu.entity.SCommodity;
import com.yidu.entity.STrolley;
import com.yidu.service.SCommodityService;
import com.yidu.service.STrolleyService;
import com.yidu.utils.BaseMessage;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (STrolley)表控制层
 *
 * @author wanghong
 * @since 2021-07-29 16:30:25
 */
@RestController
@RequestMapping("sTrolley")
public class STrolleyController {
    /**
     * 服务对象
     */
    @Autowired
    private STrolleyService sTrolleyService;

    /**
     *
     */
    @Autowired
    private SCommodityService sCommodityService;
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public STrolley selectOne(Integer id) {
        return this.sTrolleyService.queryById(id);
    }


    /**
     * 添加购物车
     * @param sTrolley
     * @return
     */
    @PostMapping("addAndUpdate")
    public Map<String,Object> addAndUpdate(STrolley sTrolley){
        STrolley sTrolley1 = sTrolleyService.findCoidAndUserId(sTrolley);
        SCommodity sCommodity = sCommodityService.queryById(sTrolley.getCoid());
        STrolley str = null;
        sTrolley.setCodate(new java.sql.Date(new Date().getTime()));
        if (sTrolley1==null){
             sTrolley.setConum(1);
             sTrolley.setCosum(sCommodity.getComoney());

             str = sTrolleyService.insert(sTrolley);
        }else {
            int num = sTrolley1.getConum()+1;
            sTrolley1.setConum(num);
            sTrolley1.setCosum(num*sCommodity.getComoney());
             str = sTrolleyService.update(sTrolley1);
        }
        if (str!=null){
            return BaseMessage.judge("1","操作成功");
        }else {
            return BaseMessage.judge("0","操作失败");
        }

    }

    /**
     * 修改数量
     * @param trid
     * @param fu
     * @return
     */
    @PostMapping("update")
    public Map<String,Object> update(Integer trid,String fu){
        STrolley sTrolley = sTrolleyService.queryById(trid);
        SCommodity sCommodity = sCommodityService.queryById(sTrolley.getCoid());
        STrolley sTrolley1 = null;
        if (fu.equals("+")){
            int num = sTrolley.getConum()+1;
            sTrolley.setConum(num);
            sTrolley.setCosum(num*sCommodity.getComoney());
            sTrolley.setCodate(new java.sql.Date(new Date().getTime()));
            sTrolley1 = sTrolleyService.update(sTrolley);

        }else {
            int rows=0;
            if (sTrolley.getConum()>1){
                int num = sTrolley.getConum()-1;
                sTrolley.setConum(num);
                sTrolley.setCosum(num*sCommodity.getComoney());
                sTrolley.setCodate(new java.sql.Date(new Date().getTime()));
                sTrolley1 = sTrolleyService.update(sTrolley);

            }
        }

        if (sTrolley1!=null){
            return BaseMessage.judge("1","操作成功");
        }else {
            return BaseMessage.judge("0","操作失败");
        }

    }

    /**
     * 从购物车移除
     * @param trid
     * @return
     */
    @PostMapping("delete")
    public Map<String,Object> delete(Integer trid){
        boolean b = sTrolleyService.deleteById(trid);
        if (b){
            return BaseMessage.judge("1","删除成功");
        }else {
            return BaseMessage.judge("0","删除失败");
        }
    }

    /**
     * 查询加入购物车的商品
     * @param userid
     * @return
     */
    @PostMapping("showList")
    public Map<String,Object> showList(Integer userid){
        List<STrolley> list = sTrolleyService.findByUserId(userid);

        return BaseMessage.judge("1","查询到的数据",list);
    }
}
